/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestProperties.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package io.api;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/**
 * java.util.Properties测试<br/>
 * 
 * Properties[配置文件类]: 主要用于产生和读取配置文件信息<br/>
 * Properties要注意的细节<br/>
 * 1. 如果使用字节流生成配置文件, 默认使用iso8859-1码表进行编码存储<br/>
 * 2. 如果配置文件的信息使用了中文, 那么在使用store方法生成配置文件时只能使用字符流解决, 否则出现乱码<br/>
 * 3. 如果Properties中的内容发生了变化, 一定要重新使用Properties生成配置文件, 否则配置文件信息不会发生变化<br/>
 * 
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestProperties {

	/**
	 * 获得系统属性
	 */
	public static Properties getSystemProperties() {
		System.out.println("获得系统的属性开始...");
		Properties systemProperties = System.getProperties();
		Set<Entry<Object, Object>> entrySet = systemProperties.entrySet();
		for (Entry<Object, Object> entry : entrySet) {
			System.out.println(entry.getKey() + "--->" + entry.getValue());
		}
		System.out.println("获得系统属性结束...");
		return systemProperties;
	}

	/**
	 * 存储属性到文件
	 * 
	 * @param propertyName
	 *            属性名
	 * @param propertyValue
	 *            属性值
	 */
	public static void storeProperties(String propertyName, String propertyValue) {
		Properties properties = new Properties();
		properties.setProperty(propertyName, propertyValue);

		FileWriter writer = null;
		try {
			writer = new FileWriter("src/__/user.properties", true);
			properties.store(writer, "this is a demo properties file");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 加载属性文件
	 * 
	 * @param propertiesFileName
	 *            属性文件名
	 */
	public static Properties loadProperties(String propertiesFileName) {
		Properties properties = new Properties();
		FileReader reader = null;

		try {
			reader = new FileReader(propertiesFileName);
			properties.load(reader);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return properties;
	}

	public static void main(String[] args) {
		Properties props = getSystemProperties();
		System.out.println();

		// 获得属性
		String userHome = props.getProperty("user.home");
		System.out.println("User home is " + userHome);
		// 设置属性
		props.setProperty("myPropKey", "myPropValue");

		storeProperties("username", "ge.y.yang@gmail.com");
		storeProperties("password", "123456");
		loadProperties("src/__/user.properties");
	}
}
